Page({
    data: {
        showArea: false,
        countdown: 0,
        countdownText: '获取验证码',
        isCountingDown: false,
        formData: {
            name: '',
            phone: '',
            smsCode: '',
            area: '',
            detail: '',
            isDefault: false
        }
    },

    onLoad(options) {
        if (options.id) {
            this.loadAddressData(options.id);
        }
    },

    loadAddressData(id) {
        // 模拟加载地址数据
        const mockAddress = {
            id: 1,
            name: '张先生',
            phone: '13800138000',
            area: '北京市 朝阳区',
            detail: '建国路88号',
            isDefault: true
        };
        this.setData({ formData: mockAddress });
    },

    showAreaPicker() {
        this.setData({ showArea: true });
    },

    onAreaConfirm(e) {
        const area = e.detail.values.join(' ');
        this.setData({
            'formData.area': area,
            showArea: false
        });
    },

    onAreaCancel() {
        this.setData({ showArea: false });
    },

    onNameChange(e) {
        this.setData({ 'formData.name': e.detail });
    },

    onPhoneChange(e) {
        this.setData({ 'formData.phone': e.detail });
    },

    onDetailChange(e) {
        this.setData({ 'formData.detail': e.detail });
    },

    onDefaultChange(e) {
        this.setData({ 'formData.isDefault': e.detail });
    },

    sendSmsCode() {
        const { phone } = this.data.formData;

        if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
            wx.showToast({ title: '请输入正确的手机号', icon: 'none' });
            return;
        }

        wx.showLoading({ title: '发送中...' });
        setTimeout(() => {
            wx.hideLoading();
            wx.showToast({ title: '验证码已发送' });
            this.startCountdown();
        }, 1000);
    },

    validateForm() {
        const { phone, smsCode } = this.data.formData;

        if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
            wx.showToast({ title: '请输入正确的手机号', icon: 'none' });
            return false;
        }

        if (!smsCode || smsCode.length !== 6) {
            wx.showToast({ title: '请输入6位验证码', icon: 'none' });
            return false;
        }

        // ...原有验证...
        return true;
    },

    startCountdown() {
        this.setData({
            countdown: 60,
            isCountingDown: true,
            countdownText: '60s后重新获取'
        });

        const timer = setInterval(() => {
            const { countdown } = this.data;
            if (countdown <= 1) {
                clearInterval(timer);
                this.setData({
                    countdownText: '获取验证码',
                    isCountingDown: false
                });
                return;
            }

            this.setData({
                countdown: countdown - 1,
                countdownText: `${countdown - 1}s后重新获取`
            });
        }, 1000);
    },

    submitAddress() {
        const { name, phone, area, detail } = this.data.formData;

        if (!name) {
            wx.showToast({ title: '请输入联系人', icon: 'none' });
            return;
        }

        if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
            wx.showToast({ title: '请输入正确手机号', icon: 'none' });
            return;
        }

        if (!area) {
            wx.showToast({ title: '请选择所在地区', icon: 'none' });
            return;
        }

        if (!detail) {
            wx.showToast({ title: '请输入详细地址', icon: 'none' });
            return;
        }

        // 模拟提交
        wx.showLoading({ title: '保存中...' });
        setTimeout(() => {
            wx.hideLoading();
            wx.showToast({ title: '保存成功' });
            wx.navigateBack();
        }, 1000);
    }
});